package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entitiy.TbCategory;
import com.leyou.item.mapper.TbCategoryMapper;
import com.leyou.item.service.TbCategoryService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TbCategoryServiceImpl extends ServiceImpl<TbCategoryMapper, TbCategory>
        implements TbCategoryService {
    @Override
    public List<CategoryDTO> queryListByParentId(Long pid) {
        QueryWrapper<TbCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbCategory::getParentId,pid);
        List<TbCategory> tbCategoryList = this.getBaseMapper().selectList(queryWrapper);

        if(CollectionUtils.isEmpty(tbCategoryList)){
            throw new LyException(ExceptionEnum.GOODS_NOT_FOUND);
        }
        List<CategoryDTO> categoryDTOS = BeanHelper.copyWithCollection(tbCategoryList, CategoryDTO.class);
        return categoryDTOS;
    }

    @Override
    public List<CategoryDTO> findCategoryListByBrandId(Long brandId) {
        List<TbCategory> tbCategoryList = this.getBaseMapper().selectCategoryByBrandId(brandId);
        if(CollectionUtils.isEmpty(tbCategoryList)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(tbCategoryList,CategoryDTO.class);
    }
}
